var _class, _temp;

import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Image, Text } from "@tarojs/components-rn";
import { toJS } from 'mobx';
import { inject, observer } from "@tarojs/mobx-rn";
import { getRuntime, servicePhoneNumber, subtime } from "../../utils/common";
import order_detailStyleSheet from "./order_detail_styles";
import icon_89 from "../../assets/img/icon_89.png";
import icon_88 from "../../assets/img/icon_88.png";
var _styleSheet = order_detailStyleSheet;
let OrderDetail = (_temp = _class = class OrderDetail extends Component {
  constructor(props) {
    super(props);

    this.state = {
      id: null,
      info: {},
      orderDetails: [],
      status: null,
      statusName: '等待付款',
      statusDesc: '请尽快完成付款',
      isEditAddress: false
    };
  }
  componentWillMount() {
    Taro.setNavigationBarTitle({ title: '订单详情' });
    let id = this.$router.params.id;
    if (!id) getRuntime().navigateBack();
    this.setState({
      id
    });
    this.getOrder(parseInt(id));
  }
  componentDidShow() {
    if (this.state.isEditAddress) {
      this.updateUserReceiver();
    }
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  getOrder(id) {
    let { PointMallStore } = this.props.stores;
    PointMallStore.getOrder({
      formId: id,
      callback: res => {
        this.setState({
          info: toJS(res),
          status: toJS(res.status),
          orderDetails: res.orderDetails[0]
        });
        this.getStatusName(res.status);
      }
    });
  }
  // 取消订单
  cancelOrdedr(id) {
    Taro.showModal({
      title: '你确定要取消订单吗?',
      content: '',
      cancelText: '再想想',
      confirmText: '确认取消'
    }).then(res => {
      if (res.confirm) {
        let { PointMallStore } = this.props.stores;
        PointMallStore.cancelOrder({
          form: { id },
          callback: () => {
            getRuntime().showToast('取消订单成功~');
            setTimeout(() => {
              getRuntime().navigateTo('/pages/user/order');
            }, 1500);
          }
        });
      }
    });
  }
  // 删除订单
  delOrder() {
    let id = this.state.id;
    if (!id) {
      getRuntime().showToast('参数错误~');
      return;
    }
    Taro.showModal({
      title: '你确定要删除订单吗?',
      content: '',
      cancelText: '再想想',
      confirmText: '确认删除'
    }).then(res => {
      if (res.confirm) {
        let { PointMallStore } = this.props.stores;
        PointMallStore.deleteOrder({
          form: { id },
          callback: () => {
            getRuntime().showToast('删除订单成功~');
            setTimeout(() => {
              getRuntime().navigateTo('/pages/user/order');
            }, 1500);
          }
        });
      }
    });
  }
  // 确认收货
  confirmGet(id) {
    let { PointMallStore } = this.props.stores;
    PointMallStore.confirmGet({
      form: { id },
      callback: () => {
        getRuntime().showToast('收货成功~');
      }
    });
  }
  // 再来一单
  againOrder() {
    let info = this.state.info;
    if (!info.id) {
      getRuntime().showToast('参数错误~');
      return;
    }
    let userCart = {
      freight: info.freight,
      itemId: info.id,
      itemNum: info.orderDetails[0]['qty'],
      itemSignPrice: info.orderDetails[0]['price'],
      itemPrice: info.orderDetails[0]['totalPrice'],
      itemSpec: info.orderDetails[0]['spec'],
      name: info.orderDetails[0]['name'],
      picture: info.orderDetails[0]['picture'],
      publicDonations: info.gyMoney,
      pointValue: info.pointValue,
      specNo: '',
      specNoId: info.orderDetails[0]['itemSpecId']
    };
    getRuntime().setStorage('userCart', userCart);
    getRuntime().navigateTo('/pages/mall/order_confirm?id=' + info.id);
    /*let { PointMallStore } = this.props.stores
    PointMallStore.placeNewOrder({
      id,
      callback: res => {
        // let payParams = JSON.parse(res.payParams)
        getRuntime().wxPay(res,() => {
          getRuntime().navigateTo(`/pages/user/order_pay_success?id=${id}`)
        })
      }
    })*/
  }
  // 立即支付
  pay() {
    let { id } = this.state;
    if (!id) {
      getRuntime().showToast('参数错误~');
      return;
    }
    /* getRuntime().commonPay(id,5,false, () => {
       getRuntime().navigateTo(`/pages/user/order_pay_success?id=${id}`)
     })*/
    let { PointMallStore } = this.props.stores;
    PointMallStore.pay({
      form: {
        orderId: id,
        payType: '1',
        isPayPoints: false
      },
      callback: res => {
        // let payParams = JSON.parse(res.payParams)
        getRuntime().wxPay(res, () => {
          getRuntime().navigateTo(`/pages/user/order_pay_success?id=${id}`);
        });
      }
    });
  }
  getStatusName(status) {
    let statusName = '';
    let statusDesc = '';
    switch (status) {
      case 1:
        {
          statusName = '等待付款';
          statusDesc = '(请尽快完成付款)';
          break;
        }
      case 2:
        {
          statusName = '等待商家发货';
          statusDesc = '(已支付)';
          break;
        }
      case 3:
        {
          statusName = '等待买家收货';
          statusDesc = '(您的包裹正在配送中...)';
          break;
        }
      case 4:
        {
          statusName = '交易成功';
          statusDesc = '';
          break;
        }
    }
    this.setState({
      statusName, statusDesc
    });
  }
  copy(con = '') {
    Taro.setClipboardData({
      data: con
    }).then(() => {
      getRuntime().showToast('复制成功~');
    });
  }
  // 修改地址按钮
  updateAddress() {
    this.setState({
      isEditAddress: true
    }, () => {
      getRuntime().navigateTo('/pages/mall/address');
    });
  }
  // 更新收货地址
  updateUserReceiver() {
    let { PointMallStore } = this.props.stores;
    let orderId = this.state.info.id;
    let userSelectAddress = getRuntime().getStorage('userSelectAddress');
    let receiverId = userSelectAddress.id;
    PointMallStore.updateUserReceiver({
      form: {
        orderId,
        receiverId
      },
      callback: () => {
        getRuntime().showToast('修改地址成功~');
        this.getOrder(orderId);
      }
    });
  }
  makePhone() {
    getRuntime().makePhoneCall(servicePhoneNumber);
  }
  render() {
    let { info, orderDetails, status, statusName, statusDesc } = this.state;
    return <View style={_styleSheet["my-order-detail"]}>
        <View style={_styleSheet["order-status"]}>
           <View style={_styleSheet["status"]}>{statusName}</View>
           <View style={_styleSheet["desc"]}>{statusDesc}</View>
        </View>

        <View style={_styleSheet["order-address"]}>
            <Image src={icon_88} style={_styleSheet["address-icon"]} />
            <View style={_styleSheet["info"]}>
              <View style={_styleSheet["list-title"]}>{info.receiver}  {info.receiverPhone}</View>
              <View style={_styleSheet["list-desc"]}>{info.receiverAddress}</View>
            </View>
          {status === 1 && <View onClick={this.updateAddress.bind(this)} style={_styleSheet["r"]}>修改</View>}
        </View>
        <View style={_styleSheet["order-spec"]}>
          <View style={_styleSheet["l"]}>
            <Image src={orderDetails.picture} style={_styleSheet["img"]} />
            <View style={_styleSheet["info"]}>
              <View style={_styleSheet["list-title"]}>{orderDetails.name}</View>
              <View style={_styleSheet["list-info"]}>
                <Text>{orderDetails.spec}</Text>
               
              </View>
            </View>
          </View>
          <View style={_styleSheet["r"]}>
            <View style={_styleSheet["list-price"]}>
              <Text style={_styleSheet["list-small"]}>￥</Text>{orderDetails.price}</View>
            <View style={[_styleSheet["list-info"], { textAlign: 'right' }]}>
              <Text>x{orderDetails.qty}</Text>
            </View>
          </View>
        </View>

        <View style={_styleSheet["order-item"]}>
          <View style={_styleSheet["line"]}>
            <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>订单金额</View>
            <View style={_styleSheet["money"]}><Text style={_styleSheet["list-small"]}>￥</Text>{info.amount}</View>
          </View>
          <View style={_styleSheet["line"]}>
            <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>运费（快递）</View>
            {info.freight > 0 && <View style={_styleSheet["money"]}><Text style={_styleSheet["list-small"]}>￥</Text>{info.freight}</View>}
            {info.freight <= 0 && <View style={_styleSheet["money"]}><Text>免费配送</Text></View>}
          </View>
          <View style={_styleSheet["all-price"]}>
           应付金额： <Text style={_styleSheet["list-small"]}>￥</Text> {info.actualAmount}
          </View>
        </View>

        <View style={[_styleSheet["order-item"], { paddingBottom: '10px' }]}>
          <View style={_styleSheet["line"]}>
            <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>订单编号</View>
            <View style={_styleSheet["money"]}>{info.orderNo}</View>
          </View>
          <View style={_styleSheet["line"]}>
            <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>下单时间</View>
            <View style={_styleSheet["money"]}>{subtime(info.creationTime)}</View>
          </View>
          {info.payTime && <View style={_styleSheet["line"]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>付款时间</View>
             <View style={_styleSheet["money"]}>{subtime(info.payTime)}</View>
          </View>}
          {info.shippingTime && <View style={_styleSheet["line"]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>发货时间</View>
             <View style={_styleSheet["money"]}>{subtime(info.shippingTime)}</View>
          </View>}
          {status === 3 && info.shippingNo && <View style={[_styleSheet["line"], _styleSheet["line2"]]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>物流快递</View>
             <View style={[_styleSheet["money"], _styleSheet["company"]]}>
                <Text style={_styleSheet["company"]}>{info.shippingNo}</Text>
                <Text onClick={this.copy.bind(this, '189887422570')} style={_styleSheet["btn-copy"]}>复制</Text>
             </View>
          </View>}
          {status !== 3 && info.shippingNo && <View style={_styleSheet["line"]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>物流快递</View>
             <View style={_styleSheet["money"]}>{info.shippingNo}</View>
          </View>}
          {info.receiveTime && <View style={_styleSheet["line"]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>收货时间</View>
             <View style={_styleSheet["money"]}>{subtime(info.receiveTime)}</View>
          </View>}
          {info.payTime && <View style={_styleSheet["line"]}>
             <View style={[_styleSheet["name"], _styleSheet["list-desc"]]}>成交时间</View>
             <View style={_styleSheet["money"]}>{subtime(info.receiveTime)}</View>
          </View>}
        </View>

        <View onClick={this.makePhone.bind(this)} style={_styleSheet["contact"]}>
          <Image src={icon_89} style={_styleSheet["img"]} />
          <View style={_styleSheet["title"]}>联系客服</View>
        </View>

        <View style={_styleSheet["footer-block"]} />
        <View style={_styleSheet["interval"]} />

        {status === 1 && <View style={[_styleSheet["page-footer2"], _styleSheet["page-footer3"]]}>
           <View style={_styleSheet["list-desc"]}>应付金额：<Text style={_styleSheet["list-small"]}>￥</Text><Text style={_styleSheet["color-orange"]}>{info.actualAmount}</Text></View>
           <View onClick={this.cancelOrdedr.bind(this, info.id)} style={_styleSheet["btn"]}>取消订单</View>
           <View onClick={this.pay.bind(this)} style={[_styleSheet["btn"], _styleSheet["btn-active"], _styleSheet["now-pay"]]}>立即付款</View>
        </View>}
        {status === 3 && <View style={[_styleSheet["page-footer2"], _styleSheet["page-footer3"]]}>
           <View onClick={this.confirmGet.bind(this, info.id)} style={[_styleSheet["btn"], _styleSheet["btn-active"]]}>确认收货</View>
        </View>}
        {status === 4 && <View style={[_styleSheet["page-footer2"], _styleSheet["page-footer3"]]}>
           <View onClick={this.againOrder.bind(this)} style={[_styleSheet["btn"], _styleSheet["btn-active"]]}>再次购买</View>
        </View>}
      </View>;
  }
}, _class.config = {
  navigationBarTitleText: '订单详情'
}, _temp);
OrderDetail = tslib_1.__decorate([inject("stores"), observer], OrderDetail);
export default OrderDetail;